t.2.sample <- function(m.1,m.2,s.1,s.2,n.1, n.2,alpha=0.05,tails=2){ # compute sigma.hat.squared df <- n.1 + n.2 -2 sigma.hat.squared <-( (n.1-1)*s.1^2 +(n.2-1)*s.2^2 )/df # compute t t <- (m.1 - m.2) / sqrt((1/n.1 + 1/n.2)*sigma.hat.squared) # compute critical value if(tails == -1) p <- alpha if(tails == 1) p <- 1-alpha if(tails == 2) p <- c(alpha/2,1 - alpha/2) crit <- qt(p,df) # create a list of named quantities and return it res <- list(t.statistic = t, df = df, alpha = alpha, critical.t.values = crit) return(res) } t.2.sample.ci <- function(m.1,m.2,s.1,s.2,n.1, n.2,conf=.95){ # compute sigma.hat.squared df <- n.1 + n.2 -2 sigma.hat.squared <-( (n.1-1)*s.1^2 +(n.2-1)*s.2^2 )/df se.diff <- sqrt((1/n.1 + 1/n.2)*sigma.hat.squared) alpha <- 1 - conf p <- 1 - alpha/2 t.crit <- qt(p,df) diff <- t.crit * se.diff lower <- m.1-m.2 - diff upper <- m.1-m.2 + diff res <- list(lower.limit=lower,upper.limit=upper,confidence.level = conf) return(res) }